home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / multcx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.6 KB  |  240 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18. ** This program creates two GLX contexts, and alternately draws into the
  19. ** same window with them.
  20. */
  21.  
  22. #define NeedFunctionPrototypes 1
  23. #include <X11/Xlib.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/keysym.h>
  26. #include <GL/gl.h>
  27. #include <GL/glx.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. /*
  33. ** Convenience routines for creating and configuring windows.
  34. **
  35. ** $Revision: 1.3 $
  36. ** $Date: 1993/08/09 22:39:51 $
  37. */
  38.  
  39. Window SetupWindow(Display *dpy, int width, int height, XVisualInfo *vi,
  40.            unsigned long background)
  41. {
  42.     int scr;
  43.     unsigned long mask;
  44.     Visual *vis = vi->visual;
  45.     Colormap cmap;
  46.     Window win, parent;
  47.     XSetWindowAttributes wa;
  48.     
  49.     scr = DefaultScreen(dpy);
  50.     parent = RootWindow(dpy, scr);
  51.  
  52.     cmap = XCreateColormap(dpy, RootWindow(dpy, scr), vis, AllocNone);
  53.  
  54.     wa.colormap = cmap;
  55.     wa.background_pixel = background;
  56.     wa.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  57.     mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
  58.     
  59.     /*
  60.     ** Create window
  61.     */
  62.     win = XCreateWindow(dpy, parent, 0, 0, width, height, 0, vi->depth,
  63.             InputOutput, vis, mask, &wa);
  64.     XMapWindow(dpy, win);
  65.     return win;
  66. }
  67.  
  68. #define WIDTH 200
  69. #define HEIGHT 200
  70.  
  71. int screen;
  72. Window win;
  73. Display *dpy;
  74. XVisualInfo *vis;
  75.  
  76. GLXContext leftContext, rightContext;
  77.  
  78. void init(void)
  79. {
  80.     glEnable(GL_SCISSOR_TEST);
  81.     glEnable(GL_DEPTH_TEST);
  82.  
  83.     glViewport(10, 10, WIDTH-20, HEIGHT-20);
  84.     glScissor(10, 10, WIDTH-20, HEIGHT-20);
  85.     glMatrixMode(GL_PROJECTION);
  86.     glLoadIdentity();
  87.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  88.     glMatrixMode(GL_MODELVIEW);
  89.  
  90.     glClearColor(0.0, 0.0, 0.0, 0.0);
  91.  
  92.     glDisable(GL_POLYGON_SMOOTH);
  93.     glDisable(GL_BLEND);
  94. }
  95.  
  96. void redraw(void)
  97. {
  98.     if (!glXMakeCurrent(dpy, win, rightContext)) {
  99.     printf("glXMakeCurrent on right context failed\n");
  100.     }
  101.  
  102.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  103.     glBegin(GL_TRIANGLES);
  104.     glVertex3f( 0.9, -0.9, -30.0);
  105.     glVertex3f( 0.9,  0.9, -30.0);
  106.     glVertex3f(-0.9,  0.0, -30.0);
  107.     glEnd();
  108.     glFlush();
  109.  
  110.     if (!glXMakeCurrent(dpy, win, leftContext)) {
  111.     printf("glXMakeCurrent on left context failed\n");
  112.     }
  113.     glBegin(GL_TRIANGLES);
  114.     glVertex3f(-0.9, -0.9, -40.0);
  115.     glVertex3f(-0.9,  0.9, -40.0);
  116.     glVertex3f( 0.9,  0.0, -25.0);
  117.     glEnd();
  118.     glFlush();
  119. }
  120.  
  121.  
  122. float colors[][3] = {
  123.     {1, 1, 0},
  124.     {0, 1, 1},
  125.     {1, 0, 1},
  126. };
  127.  
  128. void copy(void)
  129. {
  130.     GLXContext thirdContext;
  131.     int ncolors = sizeof(colors) / sizeof(colors[0]);
  132.     static int index=0;
  133.  
  134.     /*
  135.     ** Make a red context.
  136.     */
  137.     thirdContext = glXCreateContext(dpy, vis, None, GL_TRUE);
  138.     if (!glXMakeCurrent(dpy, win, thirdContext)) {
  139.     printf("glXMakeCurrent on third context failed\n");
  140.     }
  141.     init();
  142.     glColor3fv(colors[index++ % ncolors]);
  143.     glFlush();
  144.     glXMakeCurrent(dpy, None, None);
  145.     /*
  146.     ** Copy attributes of the third context to the left context.
  147.     */
  148.     glXCopyContext(dpy, thirdContext, leftContext, GL_CURRENT_BIT);
  149.     glXDestroyContext(dpy, thirdContext);
  150.     redraw();
  151. }
  152.  
  153. int attribList[] = {
  154.     GLX_RGBA, GL_TRUE,
  155.     GLX_DEPTH_SIZE, 1,
  156.     None,
  157. };
  158.  
  159. void main(int argc, char **argv)
  160. {
  161.     char buf[100];
  162.     XEvent event;
  163.     KeySym ks;
  164.     unsigned long mask;
  165.     Colormap cmap;
  166.     Window parent;
  167.     XSetWindowAttributes wa;
  168.     
  169.     dpy = XOpenDisplay(0);
  170.     if (!dpy) {
  171.     fprintf(stderr, "Can't connect to display\n");
  172.     return;
  173.     }
  174.     screen = DefaultScreen(dpy);
  175.     parent = RootWindow(dpy, screen);
  176.     vis = glXChooseVisual(dpy, screen, attribList);
  177.     if (!vis) {
  178.     fprintf(stderr, "Can't find suitable visual\n");
  179.     return;
  180.     }
  181.  
  182.     win = SetupWindow(dpy, WIDTH, HEIGHT, vis, BlackPixel(dpy,screen));
  183.  
  184.     leftContext = glXCreateContext(dpy, vis, None, GL_TRUE);
  185.     rightContext = glXCreateContext(dpy, vis, None, GL_TRUE);
  186.  
  187.     if (!glXMakeCurrent(dpy, win, leftContext)) {
  188.     printf("glXMakeCurrent on left context failed\n");
  189.     }
  190.     glColor3f(0, 1, 0);
  191.     init();
  192.     
  193.     if (!glXMakeCurrent(dpy, win, rightContext)) {
  194.     printf("glXMakeCurrent on right context failed\n");
  195.     }
  196.     glColor3f(0, 0, 1);
  197.     init();
  198.  
  199.     /*
  200.     ** Redraw when you hit 'd'.
  201.     ** Copy context when you hit 'c'.
  202.     */
  203.     while (1) {
  204.     XNextEvent(dpy, &event);
  205.     switch (event.type) {
  206.       case Expose:
  207.         if (!glXMakeCurrent(dpy, win, leftContext)) {
  208.         printf("glXMakeCurrent on left context failed\n");
  209.         }
  210.         init();
  211.         
  212.         if (!glXMakeCurrent(dpy, win, rightContext)) {
  213.         printf("glXMakeCurrent on right context failed\n");
  214.         }
  215.         init();
  216.         redraw();
  217.         break;
  218.       case KeyPress:
  219.         XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  220.         switch (ks) {
  221.           case XK_c:
  222.           case XK_C:
  223.         copy();
  224.         break;
  225.           case XK_d:
  226.           case XK_D:
  227.         redraw();
  228.         break;
  229.           case XK_Escape:
  230.         goto done;
  231.         }
  232.     }
  233.     }
  234.   done:
  235.     glXMakeCurrent(dpy, None, NULL);
  236.     glXDestroyContext(dpy, leftContext);
  237.     glXDestroyContext(dpy, rightContext);
  238.     XCloseDisplay(dpy);
  239. }
  240.